pub struct TextSegment {
    expect_length: usize,
    sentence: String,
}

impl TextSegment {
    pub fn new(expect_length: usize) -> TextSegment {
        TextSegment {
            expect_length: expect_length,
            sentence: String::new(),
        }
    }
    pub fn group(&mut self, text: &String) -> Vec<String> {
        let parts: Vec<&str> = text
            .split(|c| {
                let separators = [',', '|', ';', '.', ':', '，', '。'];
                separators.contains(&c)
            })
            .collect();

        let mut r: Vec<String> = Vec::new();
        for part in parts {
            // todo 这里计算的是字节长度
            if self.sentence.len() < self.expect_length {
                if (part.len() > 0) {
                    self.sentence.push_str(" ");
                    self.sentence.push_str(part);
                }
            } else {
                // todo 字符串交换的方法来减少复制
                r.push(self.sentence.clone());
                self.sentence = part.to_string();
            }
        }

        return r;
    }

    pub fn end(&mut self) -> Option<String> {
        let r = self.sentence.clone();
        self.sentence = "".to_string();
        if r.len() > 0 {
            return Some(r);
        } else {
            None
        }
    }
}
